/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-05-16
 * Time: 17:32
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginForm extends JFrame {
    public static  UserForm LoginuserForm;
    // 构造方法和组件添加
    public LoginForm(UserForm userForm) {
        LoginuserForm=userForm;
        // ...（设置标题、大小、默认关闭操作、布局等）
        setTitle("用户登录");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        setLocationRelativeTo(null);

        // 用户名输入
        JLabel usernameLabel = new JLabel("用户名:");
        JTextField usernameField = new JTextField(28);
        add(usernameLabel);
        add(usernameField);

        // 密码输入
        JLabel passwordLabel = new JLabel("密码:");
        JPasswordField passwordField = new JPasswordField(28);
        add(passwordLabel);
        add(passwordField);

        // 登录按钮
        JButton loginButton = new JButton("登录");
        add(loginButton);
        //注册按钮
        JButton SignButton = new JButton("注册");
        add(SignButton);

        // 为登录按钮添加事件监听器
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());

                // 这里应该从数据库或其他存储中验证用户名和密码
                if ( UserForm.isValidUser(username, password)) {
                    JOptionPane.showMessageDialog(null, "登录成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        SignButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new SignForm(LoginuserForm);
                setVisible(false);
            }
        });
        // 设置界面可见
        setVisible(true);
    }

    // 模拟验证用户名和密码的方法
}

